/**
 * Notes: 基础项目数据模型
 * Ver : CCMiniCloud Framework 2.0.1 ALL RIGHTS RESERVED BY cclinux0730 (wechat)
 * Date: 2023-08-30 19:20:00
 */

const BaseModel = require('../../../framework/platform/model/base_model.js');

class BaseProjectModel extends BaseModel {

	/**
	 * 获取列表字段
	 * @returns {string}
	 */
	getListFields() {
		return '*';
	}

	/**
	 * 获取数据列表
	 * @param {*} where 
	 * @param {*} fields 
	 * @param {*} orderBy 
	 * @param {*} page 
	 * @param {*} size 
	 * @param {*} isTotal 
	 * @param {*} oldTotal 
	 */
	async getListOfModel(service, where, fields, orderBy, page, size, isTotal, oldTotal) {
		if (!fields) fields = this.getListFields();

		if (page <= 0) page = 1;
		if (size <= 0) size = 10;
		if (!oldTotal) oldTotal = 0;

		let offset = 0;
		if (size > 0 && page > 1) {
			offset = (page - 1) * size;
		}

		let list = await this.getAll(where, fields, orderBy, offset, size);

		let total = 0;
		if (isTotal) {
			total = await this.count(where);
		}

		return {
			list,
			total,
			page,
			size,
			oldTotal
		}
	}

	/**
	 * 获取单条数据
	 * @param {*} service 
	 * @param {*} id 
	 */
	async getOneOfModel(service, id) {
		let where = {
			_id: id
		}
		return await this.getOne(where);
	}

	/**
	 * 插入数据
	 * @param {*} service 
	 * @param {*} data 
	 */
	async insertOfModel(service, data) {
		return await this.insert(data);
	}

	/**
	 * 更新数据
	 * @param {*} service 
	 * @param {*} id 
	 * @param {*} data 
	 */
	async editOfModel(service, id, data) {
		let where = {
			_id: id
		}
		return await this.edit(where, data);
	}

	/**
	 * 删除数据
	 * @param {*} service 
	 * @param {*} id 
	 */
	async delOfModel(service, id) {
		let where = {
			_id: id
		}
		return await this.del(where);
	}

	/**
	 * 更新数据状态
	 * @param {*} service 
	 * @param {*} id 
	 * @param {*} status 
	 */
	async statusOfModel(service, id, status) {
		let where = {
			_id: id
		}
		
		let data = {
			PROJECT_STATUS: status
		}
		
		return await this.edit(where, data);
	}
}

module.exports = BaseProjectModel; 